package com.ruoyi.hotel.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.hotel.domain.DeviceInfo;
import com.ruoyi.hotel.mapper.DeviceInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.RepairManageMapper;
import com.ruoyi.hotel.domain.RepairManage;
import com.ruoyi.hotel.service.IRepairManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

import static com.ruoyi.common.utils.SecurityUtils.getAuthentication;

/**
 * 维修管理Service业务层处理
 * 
 * @author zd
 * @date 2025-03-05
 */
@Service
public class RepairManageServiceImpl extends ServiceImpl<RepairManageMapper, RepairManage> implements IRepairManageService
{
    @Autowired
    private RepairManageMapper repairManageMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    /**
     * 查询维修管理
     * 
     * @param id 维修管理主键
     * @return 维修管理
     */
    @Override
    public RepairManage selectRepairManageById(String id)
    {
                return getById(id);
    }

    /**
     * 查询维修管理列表
     * 
     * @param repairManage 维修管理
     * @return 维修管理
     */
    @Override
    public List<RepairManage> selectRepairManageList(RepairManage repairManage)
    {
        return repairManageMapper.selectRepairManageList(repairManage);
    }

    /**
     * 查询维修管理列表
     *
     * @param repairManage 维修管理
     * @return 维修管理
     */
    @Override
    public List<RepairManage> manageList(RepairManage repairManage)
    {
        return repairManageMapper.manageList(repairManage);
    }

    /**
     * 查询维修管理列表
     *
     * @param repairManage 维修管理
     * @return 维修管理
     */
    @Override
    public List<RepairManage> manageUserList(RepairManage repairManage)
    {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        return repairManageMapper.manageUserList(repairManage,user.getUserId());
    }

    @Override
    public List<RepairManage> studentViewList(RepairManage repairManage) {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        LambdaQueryWrapper<RepairManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepairManage::getCreateBy,user.getUserId());
        return repairManageMapper.selectList(queryWrapper);

    }

    /**
     * 新增维修管理
     * 
     * @param repairManages 维修管理
     * @return 结果
     */
    @Override
    public int insertRepairManage(List<RepairManage> repairManages)
    {
//        repairManage.setCreateTime(DateUtils.getNowDate());
//                return save(repairManage) ? 1 : 0;
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        repairManages.forEach(repairManage->{
            repairManage.setDeviceId(repairManage.getId());
            UUID uuid = UUID.fastUUID();
            repairManage.setId(uuid.toString());
            repairManage.setDeviceStatus("2");
            repairManage.setDeviceStatusText("损坏");
            repairManage.setRepairStatus("1");
            repairManage.setRepairStatus("2");
            repairManage.setRepairStatusText("未受理");
            repairManage.setCreateTime(DateUtils.getNowDate());
            repairManage.setCreateBy(user.getUserId().toString());
            save(repairManage);
            DeviceInfo deviceInfo = new DeviceInfo();
            BeanUtils.copyProperties(repairManage, deviceInfo);
            deviceInfo.setId(repairManage.getDeviceId());
            deviceInfoMapper.updateById(deviceInfo);
        });
        return 1;
    }

    /**
     * 修改维修管理
     * 
     * @param repairManage 维修管理
     * @return 结果
     */
    @Override
    public int updateRepairManage(RepairManage repairManage)
    {
        String repairStatus = repairManage.getRepairStatus();
        if (repairStatus.equals("1")){
            repairManage.setRepairStatusText("驳回");
        }
        if (repairStatus.equals("2")){
            repairManage.setRepairStatusText("未受理");
        }
        if (repairStatus.equals("3")){
            repairManage.setRepairStatusText("已受理");
            repairManage.setDeviceStatus("3");
            repairManage.setDeviceStatusText("正在维修");
            String deviceId = repairManage.getDeviceId();
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setId(deviceId);
            deviceInfo.setDeviceStatus("3");
            deviceInfo.setDeviceStatusText("正在维修");
            deviceInfoMapper.updateById(deviceInfo);
        }
        if (repairStatus.equals("4")){
            repairManage.setRepairStatusText("完成");
            repairManage.setDeviceStatus("4");
            repairManage.setDeviceStatusText("正常");
            String deviceId = repairManage.getDeviceId();
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setId(deviceId);
            deviceInfo.setDeviceStatus("4");
            deviceInfo.setDeviceStatusText("正常");
            deviceInfoMapper.updateById(deviceInfo);
        }
        repairManage.setUpdateTime(DateUtils.getNowDate());
                return updateById(repairManage) ? 1 : 0;
    }

    /**
     * 批量删除维修管理
     * 
     * @param ids 需要删除的维修管理主键
     * @return 结果
     */
    @Override
    public int deleteRepairManageByIds(String[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除维修管理信息
     * 
     * @param id 维修管理主键
     * @return 结果
     */
    @Override
    public int deleteRepairManageById(String id)
    {
                return removeById(id) ? 1 : 0;
    }
}
